﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class User_power :BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserPower.CheckAction((int)FreeHouse.Power.Actions.TUser.EditRole);
        if (!IsPostBack)
            BindData();
    }

    protected void BindData()
    {
        roles.DataSource = new FreeHouse.BLL.TRole().GetAllList();
        roles.DataBind();
    }
    protected void roles_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item|| e.Item.ItemType== ListItemType.AlternatingItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            Repeater actions = (Repeater)e.Item.FindControl("actions");
            int roleId = int.Parse(drv.Row["RoleId"].ToString());
            actions.DataSource = new FreeHouse.BLL.TRole().GetRoleAction(roleId);
            actions.DataBind();

            Repeater fileds = (Repeater)e.Item.FindControl("fields");
            fileds.DataSource = new FreeHouse.BLL.TRole().GetRoleFields(roleId);
            fileds.DataBind();
        }
    }
    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/Admin/AdminList.aspx");
    }
    protected void save_Click(object sender, EventArgs e)
    {
        int userid;
        int.TryParse(Request.QueryString["userid"], out userid);
        if (userid == 0)
        {
            Response.Write("用户不存在.");
            Response.Redirect("~/Admin/Admin/AdminList.aspx");
        }
        //模型
        FreeHouse.Model.TUserRoles userRole = new FreeHouse.Model.TUserRoles();
        userRole.UserID = userid;
        //删除
        FreeHouse.BLL.TUserRoles bllUserRole = new FreeHouse.BLL.TUserRoles();
        bllUserRole.Delete(userRole.UserID);

        FreeHouse.Model.TUserExceptAction exceptActionModel = new FreeHouse.Model.TUserExceptAction();
        FreeHouse.BLL.TUserExceptAction exceptActionBll = new FreeHouse.BLL.TUserExceptAction();
        exceptActionBll.Delete(userRole.UserID);
        FreeHouse.Model.TUserExceptField exceptFieldModel = new FreeHouse.Model.TUserExceptField();
        FreeHouse.BLL.TUserExceptField exceptFieldBll = new FreeHouse.BLL.TUserExceptField();
        exceptActionBll.Delete(userRole.UserID);
        exceptFieldBll.Delete(userRole.UserID);
        if (!string.IsNullOrEmpty(Request.Form["role"]))
        {
            string[] roles = Request.Form["role"].Split(',');
            foreach (string s in roles)
            {
                userRole.RoleID = int.Parse(s);
                bllUserRole.Add(userRole);
                //添加用户勾选的排除权限Action
                if (!string.IsNullOrEmpty(Request.Form["action" + userRole.RoleID.ToString()]))
                {
                    string[] actions = Request.Form["action" + userRole.RoleID.ToString()].Split(',');
                    exceptActionModel.UserId = userid;
                    exceptActionModel.RoleId = userRole.RoleID;
                    
                    foreach (string actionid in actions)
                    {
                        exceptActionModel.ActionId = int.Parse(actionid);
                        exceptActionBll.Add(exceptActionModel);
                    }
                }


                //添加用户勾选的排除权限Field
                if (!string.IsNullOrEmpty(Request.Form["field" + userRole.RoleID.ToString()]))
                {
                    string[] fields = Request.Form["field" + userRole.RoleID.ToString()].Split(',');
                    exceptFieldModel.UserId = userid;
                    exceptFieldModel.RoleId = userRole.RoleID;

                    foreach (string fieldid in fields)
                    {
                        exceptFieldModel.FieldId = int.Parse(fieldid);
                        exceptFieldBll.Add(exceptFieldModel);
                    }
                }
            }
        }
        Response.Write("<script>alert('信息保存成功.')</script>");
    }
}
